package com.beyond.market.favorites.controller;

import com.beyond.market.favorites.domain.Favorites;
import com.beyond.market.favorites.service.FavoritesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.SQLException;
import java.util.List;

@Controller
@RequestMapping("/favorites")
public class FavoritesController {

    @Autowired
    private FavoritesService favoritesService;

    @RequestMapping("/index")
    public String index(){
        return "index";
    }

    @RequestMapping("/detail")
    public String detail(){
        return "detail";
    }

    @RequestMapping("/save")
    @ResponseBody
    public int save(Favorites favorites)throws  SQLException{
        int i = favoritesService.save(favorites);
        return i;
    }

    @RequestMapping("/remove/{favoritesId}")
    @ResponseBody
    public int remove(@PathVariable("favoritesId") Integer favoritesId) throws SQLException {
        int remove = favoritesService.remove(favoritesId);
        return remove;
    }

    @RequestMapping("/update")
    @ResponseBody
    public int update(Favorites favorites) throws SQLException {
        int update = favoritesService.update(favorites);
        return update;
    }

    @RequestMapping("/getByPrimaryKey/{favoritesId}")
    @ResponseBody
    public Favorites getByPrimaryKey(@PathVariable("favoritesId") Integer favoritesId, Model model) throws SQLException {

        return favoritesService.getByPrimaryKey(favoritesId);
    }

    @RequestMapping("/getByUser/{id}")
    @ResponseBody
    public List<Favorites> grtByUser(@PathVariable Integer id, ModelMap map)throws SQLException {
        List<Favorites> list = favoritesService.getByUser(id);

        return list;
    }
}
